package cn.pangza.user.service.impl;

import cn.pangza.mybatisplus.entity.PageResult;
import cn.pangza.user.convert.UserConvert;
import cn.pangza.user.dto.CreateUserDto;
import cn.pangza.user.dto.UpdateUserDto;
import cn.pangza.user.entity.UserEntity;
import cn.pangza.user.mapper.UserMapper;
import cn.pangza.user.service.UserService;
import cn.pangza.mybatisplus.entity.PageRequest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public void createUser(CreateUserDto createUserDto) {
        UserEntity user = UserConvert.INSTANCE.convertCreateUserDtoToUser(createUserDto);
//        UserEntity user = new UserEntity();
//        BeanUtils.copyProperties(createUserDto, user);
//        user.setCreatetime(new Date());
//        user.setCreateby("sys");
        this.save(user);
    }

    @Override
    public void updateUser(UpdateUserDto updateUserDto) {
        UserEntity user = new UserEntity();
        BeanUtils.copyProperties(updateUserDto, user);
//        user.setUpdateby("sys");
//        user.setUpdatetime(new Date());
        this.updateById(user);
    }

    @Override
    public PageResult<UserEntity> getUserPage(PageRequest<UserEntity> pageRequest) {
        IPage<UserEntity> userEntityIPage = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        return PageResult.generatePage(userMapper.getUserPage(userEntityIPage));
    }
}
